package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.enums.Dimensions_cha;
import com.tanhua.enums.Dimensions_par;
import com.tanhua.mapper.CharacterMapper;
import com.tanhua.mapper.ReportMapper;
import com.tanhua.mapper.UserInfoMapper;
import com.tanhua.pojo.*;
import com.tanhua.pojo.Character;
import com.tanhua.server.vo.ReportVo;
import com.tanhua.utils.UserThreadLocal;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.*;

@Service
public class ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private CharacterMapper characterMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public ReportVo queryReportReslut(Long reportId) {
        //通过report拿到userID
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_id", reportId);
        Report report = this.reportMapper.selectOne(queryWrapper);

        Long userId = report.getUserId();
        Integer star = report.getStar();
        Date createTime = report.getCreateTime();
        //生成一个报告
        ReportVo reportVo = new ReportVo();
        //根据userID 得到人格表中头像和对用户测试的评价信息
        Character character = this.getType(userId, star, createTime);
        String conclusion = character.getDescription();

        report.setCharacterId(character.getCharacterId());
        this.reportMapper.update(report,queryWrapper);

        Integer characterId = report.getCharacterId();
        reportVo.setUserId(userId);
        reportVo.setConclusion(conclusion);
        reportVo.setCover(character.getImage());

        //通过userId 得到维度信息
        List<Map<String, String>> dimensionsMap = this.getDimensions(userId, star, createTime);
        reportVo.setDimensions(dimensionsMap);


        List<Long> similarIdList = new ArrayList<>();
        QueryWrapper<Report> queryUserInfo = new QueryWrapper<>();
        queryUserInfo.eq("character_id", characterId).eq("star", star);
        List<Report> reportList = this.reportMapper.selectList(queryUserInfo);
        for (Report rep : reportList) {
            similarIdList.add(rep.getUserId());
        }

        if (CollUtil.isEmpty(similarIdList)){
            return reportVo;
        }
        //这份报告吧自己的ID排除掉 就是有相仿的人
        QueryWrapper<UserInfo> queryWrapperUserInfo = new QueryWrapper<>();
        queryWrapperUserInfo.in("user_id", similarIdList);

        List<UserInfo> userInfoList = this.userInfoMapper.selectList(queryWrapperUserInfo);

        List<SimilarYou> similarYouList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            if (UserThreadLocal.get().getId().equals(userInfo.getUserId())) {
                continue;
            }
            SimilarYou similarYou = new SimilarYou();
            similarYou.setSimilarId(userInfo.getUserId());
            similarYou.setAvatar(userInfo.getLogo());
            similarYouList.add(similarYou);
        }
        reportVo.setSimilarYou(similarYouList);

        return reportVo;
    }

    /*
     *   通过分数拿到人格数据
     * */
    private Character getType(Long userId, Integer star, Date createTime) {

        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("star", star).eq("create_time", createTime);

        Report report = this.reportMapper.selectOne(queryWrapper);
        Integer totalScore = report.getTotalScore();

        if (totalScore < 21) {
            totalScore = 1;
        }
        if (totalScore < 40 && totalScore >= 21) {
            totalScore = 2;
        }
        if (totalScore >= 40 && totalScore < 56) {
            totalScore = 3;
        }
        if (totalScore >= 56) {
            totalScore = 4;
        }
        //根据ID茶
        QueryWrapper<Character> queryWrapperCgaracter = new QueryWrapper<>();
        switch (totalScore) {
            case 1: {
                queryWrapperCgaracter.eq("character_id", "1");
                Character character = this.characterMapper.selectOne(queryWrapperCgaracter);
                return character;
            }
            case 2: {
                queryWrapperCgaracter.eq("character_id", "2");
                Character character = this.characterMapper.selectOne(queryWrapperCgaracter);
                return character;
            }
            case 3: {
                queryWrapperCgaracter.eq("character_id", "3");
                Character character = this.characterMapper.selectOne(queryWrapperCgaracter);
                return character;
            }
            case 4: {
                queryWrapperCgaracter.eq("character_id", "4");
                Character character = this.characterMapper.selectOne(queryWrapperCgaracter);
                return character;
            }
            default: {
                return null;

            }
        }
    }


    /*
     *   通过id查询维度信息
     * */
    private  List<Map<String, String>> getDimensions(Long userId, Integer star, Date createTime) {
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("star", star).eq("create_time", createTime);

        Report report = this.reportMapper.selectOne(queryWrapper);
        Integer totalScore = report.getTotalScore();


        List<Map<String, String>> mapList = new ArrayList<>();
        if (totalScore < 21) {
            Map<String, String> map1 = new HashMap<>();
            map1.put("key", Dimensions_par.OUTGO.toString());
            map1.put("value", Dimensions_cha.ONE.toString());
            Map<String, String> map2 = new HashMap<>();
            map2.put("key", Dimensions_par.ABSTRACT.toString());
            map2.put("value", Dimensions_cha.TWO.toString());
            Map<String, String> map3 = new HashMap<>();
            map3.put("key", Dimensions_par.JUDGEMENT.toString());
            map3.put("value", Dimensions_cha.THREE.toString());
            Map<String, String> map4 = new HashMap<>();
            map4.put("key", Dimensions_par.REASON.toString());
            map4.put("value", Dimensions_cha.FOUR.toString());
            mapList.add(map1);
            mapList.add(map2);
            mapList.add(map3);
            mapList.add(map4);

        }
        if (totalScore < 40 && totalScore >= 21) {
            Map<String, String> map1 = new HashMap<>();
            map1.put("key", Dimensions_par.OUTGO.toString());
            map1.put("value", Dimensions_cha.ONE.toString());
            Map<String, String> map2 = new HashMap<>();
            map2.put("key", Dimensions_par.ABSTRACT.toString());
            map2.put("value", Dimensions_cha.TWO.toString());
            Map<String, String> map3 = new HashMap<>();
            map3.put("key", Dimensions_par.JUDGEMENT.toString());
            map3.put("value", Dimensions_cha.THREE.toString());
            Map<String, String> map4 = new HashMap<>();
            map4.put("key", Dimensions_par.REASON.toString());
            map4.put("value", Dimensions_cha.FOUR.toString());
            mapList.add(map1);
            mapList.add(map2);
            mapList.add(map3);
            mapList.add(map4);
        }
        if (totalScore >= 40 && totalScore < 56) {
            Map<String, String> map1 = new HashMap<>();
            map1.put("key", Dimensions_par.OUTGO.toString());
            map1.put("value", Dimensions_cha.TWO.toString());
            Map<String, String> map2 = new HashMap<>();
            map2.put("key", Dimensions_par.ABSTRACT.toString());
            map2.put("value", Dimensions_cha.ONE.toString());
            Map<String, String> map3 = new HashMap<>();
            map3.put("key", Dimensions_par.JUDGEMENT.toString());
            map3.put("value", Dimensions_cha.THREE.toString());
            Map<String, String> map4 = new HashMap<>();
            map4.put("key", Dimensions_par.REASON.toString());
            map4.put("value", Dimensions_cha.FOUR.toString());
            mapList.add(map1);
            mapList.add(map2);
            mapList.add(map3);
            mapList.add(map4);
        }
        if (totalScore >= 56) {
            Map<String, String> map1 = new HashMap<>();
            map1.put("key", Dimensions_par.OUTGO.toString());
            map1.put("value", Dimensions_cha.FOUR.toString());
            Map<String, String> map2 = new HashMap<>();
            map2.put("key", Dimensions_par.ABSTRACT.toString());
            map2.put("value", Dimensions_cha.THREE.toString());
            Map<String, String> map3 = new HashMap<>();
            map3.put("key", Dimensions_par.JUDGEMENT.toString());
            map3.put("value", Dimensions_cha.TWO.toString());
            Map<String, String> map4 = new HashMap<>();
            map4.put("key", Dimensions_par.REASON.toString());
            map4.put("value", Dimensions_cha.ONE.toString());
            mapList.add(map1);
            mapList.add(map2);
            mapList.add(map3);
            mapList.add(map4);
        }
        return mapList;
    }
}

